# -*- makefile -*-

GRUB_KERNEL_MACHINE_LINK_ADDR = 0x8200

COMMON_CFLAGS = -mrtd -mregparm=3

# Images.
pkglib_IMAGES = boot.img cdboot.img diskboot.img pxeboot.img lnxboot.img

# For boot.img.
boot_img_SOURCES = boot/i386/pc/boot.S
boot_img_ASFLAGS = $(COMMON_ASFLAGS)
boot_img_LINKADDR = 0x7C00

# For pxeboot.img
pxeboot_img_SOURCES = boot/i386/pc/pxeboot.S
pxeboot_img_ASFLAGS = $(COMMON_ASFLAGS)
pxeboot_img_LINKADDR = 0x7C00

# For diskboot.img.
diskboot_img_SOURCES = boot/i386/pc/diskboot.S
diskboot_img_ASFLAGS = $(COMMON_ASFLAGS)
diskboot_img_LINKADDR = 0x8000

# For lnxboot.img.
lnxboot_img_SOURCES = boot/i386/pc/lnxboot.S
lnxboot_img_ASFLAGS = $(COMMON_ASFLAGS)
lnxboot_img_LINKADDR = 0x6000

# For cdboot.img.
cdboot_img_SOURCES = boot/i386/pc/cdboot.S
cdboot_img_ASFLAGS = $(COMMON_ASFLAGS)
cdboot_img_LINKADDR = 0x7C00

# Utilities.
bin_UTILITIES = grub-mkimage
sbin_UTILITIES = grub-setup

# For grub-mkimage.
grub_mkimage_SOURCES = gnulib/progname.c util/grub-mkrawimage.c \
	util/misc.c util/resolve.c lib/LzmaEnc.c lib/LzFind.c util/obj.c \
	kern/list.c
grub_mkimage_CFLAGS = -DGRUB_KERNEL_MACHINE_LINK_ADDR=$(GRUB_KERNEL_MACHINE_LINK_ADDR)
util/grub-mkrawimage.c_DEPENDENCIES = Makefile

# For grub-setup.
util/i386/pc/grub-setup.c_DEPENDENCIES = grub_setup_init.h
grub_setup_SOURCES = gnulib/progname.c \
	util/i386/pc/grub-setup.c util/hostdisk.c	\
	util/misc.c util/getroot.c kern/device.c kern/disk.c	\
	kern/err.c kern/misc.c kern/parser.c kern/partition.c	\
	kern/file.c kern/fs.c kern/env.c kern/list.c		\
	fs/fshelp.c						\
	\
	fs/affs.c fs/cpio.c fs/ext2.c fs/fat.c fs/hfs.c		 \
	fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c	 \
	fs/nilfs2.c fs/ntfs.c fs/ntfscomp.c fs/reiserfs.c	\
	fs/sfs.c fs/ufs.c fs/ufs2.c fs/xfs.c fs/afs.c		\
	fs/afs_be.c fs/befs.c fs/befs_be.c fs/tar.c		\
	\
	partmap/msdos.c partmap/bsdlabel.c partmap/sunpc.c	\
	partmap/gpt.c		\
	\
	disk/raid.c disk/mdraid_linux.c disk/lvm.c		\
	util/raid.c util/lvm.c util/mm.c			\
	grub_setup_init.c

sbin_SCRIPTS += grub-install
grub_install_SOURCES = util/grub-install.in

bin_SCRIPTS += grub-mkrescue
grub_mkrescue_SOURCES = util/grub-mkrescue.in

pkglib_MODULES = kernel.mod biosdisk.mod chain.mod	\
	halt.mod				\
	vbe.mod vbetest.mod vbeinfo.mod 		\
	vga.mod				\
	aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod  	\
	ata_pthru.mod hdparm.mod 		\
	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod \
	efiemu.mod mmap.mod acpi.mod drivemap.mod loadbin.mod

# For kernel.mod.
kernel_mod_SOURCES = kern/i386/pc/startup.S \
	kern/i386/misc.S \
	kern/main.c kern/device.c \
	kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \
	kern/misc.c kern/mm.c kern/term.c \
	kern/rescue_parser.c kern/rescue_reader.c \
	kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \
	kern/i386/pc/init.c kern/i386/pc/mmap.c \
	kern/parser.c kern/partition.c \
	kern/i386/tsc.c kern/i386/pit.c \
	kern/generic/rtc_get_time_ms.c \
	kern/generic/millisleep.c \
	kern/env.c \
	term/i386/pc/console.c term/i386/vga_common.c
kernel_mod_CFLAGS = $(COMMON_CFLAGS)  $(TARGET_IMG_CFLAGS)
kernel_mod_ASFLAGS = $(COMMON_ASFLAGS)

# For drivemap.mod.
drivemap_mod_SOURCES = commands/i386/pc/drivemap.c \
                       commands/i386/pc/drivemap_int13h.S
drivemap_mod_ASFLAGS = $(COMMON_ASFLAGS)
drivemap_mod_CFLAGS = $(COMMON_CFLAGS)
drivemap_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For efiemu.mod.
efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \
		     efiemu/i386/loadcore64.c efiemu/i386/pc/cfgtables.c \
		     efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \
		     efiemu/loadcore32.c efiemu/loadcore64.c \
		     efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \
		     efiemu/i386/coredetect.c
efiemu_mod_CFLAGS = $(COMMON_CFLAGS)
efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For acpi.mod.
acpi_mod_SOURCES = commands/acpi.c commands/i386/pc/acpi.c
acpi_mod_CFLAGS = $(COMMON_CFLAGS)
acpi_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For mmap.mod.
mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
		   mmap/i386/pc/mmap.c mmap/i386/pc/mmap_helper.S
mmap_mod_CFLAGS = $(COMMON_CFLAGS)
mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)

# For biosdisk.mod.
biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
biosdisk_mod_CFLAGS = $(COMMON_CFLAGS)
biosdisk_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For chain.mod.
chain_mod_SOURCES = loader/i386/pc/chainloader.c
chain_mod_CFLAGS = $(COMMON_CFLAGS)
chain_mod_LDFLAGS = $(COMMON_LDFLAGS)

pkglib_MODULES += linux16.mod
linux16_mod_SOURCES = loader/i386/pc/linux.c
linux16_mod_CFLAGS = $(COMMON_CFLAGS)
linux16_mod_LDFLAGS = $(COMMON_LDFLAGS)

pkglib_MODULES += linux.mod
linux_mod_SOURCES = loader/i386/linux.c
linux_mod_CFLAGS = $(COMMON_CFLAGS)
linux_mod_LDFLAGS = $(COMMON_LDFLAGS)

pkglib_MODULES += xnu.mod
xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/pc/xnu.c \
	 loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c
xnu_mod_CFLAGS = $(COMMON_CFLAGS)
xnu_mod_LDFLAGS = $(COMMON_LDFLAGS)
xnu_mod_ASFLAGS = $(COMMON_ASFLAGS)

# For halt.mod.
halt_mod_SOURCES = commands/i386/pc/halt.c
halt_mod_CFLAGS = $(COMMON_CFLAGS)
halt_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For vbe.mod.
vbe_mod_SOURCES = video/i386/pc/vbe.c
vbe_mod_CFLAGS = $(COMMON_CFLAGS)
vbe_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For vbeinfo.mod.
vbeinfo_mod_SOURCES = commands/i386/pc/vbeinfo.c
vbeinfo_mod_CFLAGS = $(COMMON_CFLAGS)
vbeinfo_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For vbetest.mod.
vbetest_mod_SOURCES = commands/i386/pc/vbetest.c
vbetest_mod_CFLAGS = $(COMMON_CFLAGS)
vbetest_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For vga.mod.
vga_mod_SOURCES = video/i386/pc/vga.c
vga_mod_CFLAGS = $(COMMON_CFLAGS)
vga_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For aout.mod
aout_mod_SOURCES = loader/aout.c
aout_mod_CFLAGS = $(COMMON_CFLAGS)
aout_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For bsd.mod
bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c loader/i386/bsd_helper.S loader/i386/bsd_trampoline.S
bsd_mod_CFLAGS = $(COMMON_CFLAGS)
bsd_mod_LDFLAGS = $(COMMON_LDFLAGS)
bsd_mod_ASFLAGS = $(COMMON_ASFLAGS)

# For usb.mod
usb_mod_SOURCES = bus/usb/usb.c bus/usb/usbtrans.c bus/usb/usbhub.c
usb_mod_CFLAGS = $(COMMON_CFLAGS)
usb_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For usbtest.mod
usbtest_mod_SOURCES = commands/usbtest.c
usbtest_mod_CFLAGS = $(COMMON_CFLAGS)
usbtest_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For uhci.mod
uhci_mod_SOURCES = bus/usb/uhci.c
uhci_mod_CFLAGS = $(COMMON_CFLAGS)
uhci_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For ohci.mod
ohci_mod_SOURCES = bus/usb/ohci.c
ohci_mod_CFLAGS = $(COMMON_CFLAGS)
ohci_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For usbms.mod
usbms_mod_SOURCES = disk/usbms.c
usbms_mod_CFLAGS = $(COMMON_CFLAGS)
usbms_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For usb_keyboard.mod
usb_keyboard_mod_SOURCES = term/usb_keyboard.c
usb_keyboard_mod_CFLAGS = $(COMMON_CFLAGS)
usb_keyboard_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For pxe.mod
pxe_mod_SOURCES = fs/i386/pc/pxe.c
pxe_mod_CFLAGS = $(COMMON_CFLAGS)
pxe_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For pxecmd.mod
pxecmd_mod_SOURCES = commands/i386/pc/pxecmd.c
pxecmd_mod_CFLAGS = $(COMMON_CFLAGS)
pxecmd_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For pxecfg.mod
pxecfg_mod_SOURCES = commands/i386/pc/pxecfg.c
pxecfg_mod_CFLAGS = $(COMMON_CFLAGS)
pxecfg_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For datetime.mod
datetime_mod_SOURCES = lib/cmos_datetime.c
datetime_mod_CFLAGS = $(COMMON_CFLAGS)
datetime_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For ata_pthru.mod.
ata_pthru_mod_SOURCES = disk/ata_pthru.c
ata_pthru_mod_CFLAGS = $(COMMON_CFLAGS)
ata_pthru_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For hdparm.mod.
hdparm_mod_SOURCES = commands/hdparm.c
hdparm_mod_CFLAGS = $(COMMON_CFLAGS)
hdparm_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For loadbin.mod.
loadbin_mod_SOURCES = loader/i386/pc/loadbin.c
loadbin_mod_CFLAGS = $(COMMON_CFLAGS)
loadbin_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For vmenu.mod.
pkglib_MODULES += vmenu.mod
vmenu_mod_SOURCES = commands/i386/pc/vmenu.c
vmenu_mod_CFLAGS = $(COMMON_CFLAGS)
vmenu_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For 915resolution.mod.
pkglib_MODULES += 915resolution.mod
915resolution_mod_SOURCES = commands/i386/pc/915resolution.c
915resolution_mod_CFLAGS = $(COMMON_CFLAGS)
915resolution_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For geom.mod.
pkglib_MODULES += geom.mod
geom_mod_SOURCES = commands/i386/pc/geometry.c
geom_mod_CFLAGS = $(COMMON_CFLAGS)
geom_mod_LDFLAGS = $(COMMON_LDFLAGS)

ifeq ($(enable_efiemu), yes)

efiemu32.o: efiemu/runtime/efiemu.c $(TARGET_OBJ2ELF)
	-rm -f $@
ifeq ($(TARGET_APPLE_CC), 1)
	-rm -f $@.bin
	$(TARGET_CC) -c -m32 -DELF32 -DAPPLE_CC -o $@.bin -Wall -Werror $< -nostdlib -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude
	$(OBJCONV) -felf32 -nu -nd $@.bin $@
	-rm -f $@.bin
else
	$(TARGET_CC) -c -m32 -DELF32 -o $@ -Wall -Werror $< -nostdlib -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude
	if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi
endif

efiemu64_c.o: efiemu/runtime/efiemu.c
ifeq ($(TARGET_APPLE_CC), 1)
	$(TARGET_CC) -c -m64 -DAPPLE_CC=1 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude
else
	$(TARGET_CC) -c -m64 -DELF64 -o $@ -Wall -Werror $< -nostdlib  -mcmodel=large -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude
endif

efiemu64_s.o: efiemu/runtime/efiemu.S
	-rm -f $@
ifeq ($(TARGET_APPLE_CC), 1)
	$(TARGET_CC) -c -m64 -DAPPLE_CC=1 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude
else
	$(TARGET_CC) -c -m64 -DELF64 -o $@ -Wall -Werror $< -nostdlib  -mcmodel=large -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude
endif

efiemu64.o: efiemu64_c.o efiemu64_s.o  $(TARGET_OBJ2ELF)
	-rm -f $@
ifeq ($(TARGET_APPLE_CC), 1)
	-rm -f $@.bin
	$(TARGET_CC) -m64 -o $@.bin -Wl,-r $^ -nostdlib
	$(OBJCONV) -felf64 -nu -nd $@.bin $@
	-rm -f $@.bin
else
	$(TARGET_CC) -m64 -o $@ -Wl,-r $^ -nostdlib
	if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi
endif

CLEANFILES += efiemu32.o efiemu64.o efiemu64_c.o efiemu64_s.o
pkglib_DATA += efiemu32.o efiemu64.o

endif

include $(srcdir)/conf/i386.mk
include $(srcdir)/conf/common.mk
